package nku.csc460;

import java.net.*;
import java.io.*;

class fooserver1 {
	//Server fields
	ServerSocket MyService = null;
    Socket serviceSocket = null;
    DataInputStream inputStream = null;
    BufferedReader input = null;
    PrintWriter output = null;
    		
    //Server Constructor		
    public fooserver1() 
    {
    	startServer();
    }
    
    //Main Method
    public static void main(String... Args)
    {
    	fooserver1 gameServer = new fooserver1();
    	Game gameInstance = new Game();
    	gameServer.finishConstructorOperations();
    	int score = 0;
    	for(int i = 0; i < 10; i++){
    		String answer = null;
    		int questionScore = 0;
    		String correctAnswer = null;
    		gameServer.sendMessage(gameInstance.getQuestionAndAnswerChoices(i));
    		answer = gameServer.readLine();
    		questionScore = gameInstance.getScore(i, answer);
    		score += questionScore;
    		gameServer.sendMessage("The In-Correct Answer is: " + gameInstance.getInCorrectAnswer(i));
    		gameServer.sendMessage("Your score for the question is: " + questionScore);
    		gameServer.sendMessage("Your Current Score is: " + score);
    	}
    	gameServer.sendMessage("Game Over!!");
    	gameServer.sendMessage("Your final score is: " + score);
    	try{
  			Thread.currentThread().sleep(10000);
    	}catch(Exception e){
    		System.out.println(e);
    	}
    }
    
    public void finishConstructorOperations(){
    	waitForConnection();
    	openInputStream();
    	openOutputStream();
    }
    
    //Open a server socket
    public void startServer(){
    	try{
			MyService = new ServerSocket(8241);
			System.out.println("Started the server! ");
		}
		catch(IOException e){
			System.out.println(e);
			System.out.println("Starting the server failed! ");
			System.exit(1);
		}
    }
    
    //Wait for connection
    public void waitForConnection(){
    	try{
			serviceSocket = MyService.accept();
			System.out.println("I have recieved a connection! ");
		}
		catch(IOException e){
			System.out.println(e);
			System.out.println("Recieving a connection failed! ");
			System.exit(1);
		}
    }
    
    //Open an input stream
    public void openInputStream(){
    	try{
			inputStream = new DataInputStream(serviceSocket.getInputStream());
			input = new BufferedReader(new InputStreamReader(inputStream));
			System.out.println("The input stream is opened to recieve messages from client!");
		}
		catch(IOException e){
			System.out.println(e);
			System.out.println("Failed to open an input stream to recieve messages from client!");
			System.exit(1);
		}
    }
    
    //Open an output stream
    public void openOutputStream(){
		try 
		{
			output = new PrintWriter(serviceSocket.getOutputStream());
			System.out.println("The output stream is opened to send messages to client!");
		}
		catch(IOException e){
			System.out.println(e);
			System.out.println("Failed to open stream to send messages to client!");
			System.exit(1);
		}    	
    }
    
    //Method to send messages to client
    public void sendMessage(String... lines){
    	for(String line: lines)
    	{
    		output.println(line);
    	}
    	output.flush();
    }
    
    //Method to read messages from client
    public String readLine(){
    	String line = null;
    	try
    	{
    		line = input.readLine();
			System.out.println(line);	
		}
		catch(IOException e)
		{
			System.out.println(e);
			System.out.println("Communication Failed!");
			System.exit(1);
		}
		return line;
    }    
}

class Game{
	String inCorrectAnswers[] = new String[10];
	String questions[] = new String[10];
	String option1[] = new String[10];
	String option2[] = new String[10];
	String option3[] = new String[10];
	String option4[] = new String[10];
	
	Game(){
		BufferedReader commandLine = new BufferedReader(new InputStreamReader(System.in));
		String filename = null;
		try{
			System.out.print("Enter a filename: ");
			filename = commandLine.readLine();
		}
		catch(IOException e)
		{
			System.out.println(e);
			System.out.println("Cannot read file name!");
			System.out.println("If you can't enter a correct file name you won't continue i will crash!");
			System.exit(1);
		}
		try{
			FileReader fr = new FileReader(filename);
			BufferedReader brFile = new BufferedReader(fr);
			for(int i = 0; i < 10; i++){
				questions[i] = brFile.readLine();
				option1[i] = brFile.readLine();
				option2[i] = brFile.readLine();
				option3[i] = brFile.readLine();
				option4[i] = brFile.readLine();
				inCorrectAnswers[i] = brFile.readLine();
			}
		}
		catch(IOException e){
			System.out.println(e);
			System.out.println("Error Reading File! ");
			System.exit(1);
		}
		
	}
	
	public String[] getQuestionAndAnswerChoices(int i){
		String temp[] = new String [5];
		temp[0] = questions[i];
		temp[1] = option1[i];
		temp[2] = option2[i];
		temp[3] = option3[i];
		temp[4] = option4[i];
		return temp;
	}
	
	public int getScore(int i, String answer){
		if(answer.contains(inCorrectAnswers[i])){
			return -250;
		}
		else{
			return 250*answer.length();
		}
	}
	
	public String getInCorrectAnswer(int i){
		return inCorrectAnswers[i];
	}
	
}